// config/config.go

package config

import (
	"os"
	"strconv"
)

type Config struct {
	ServerPort     int
	ModbusPort     int
	APIKey         string
	UpdateInterval int
}

func LoadConfig() *Config {
	return &Config{
		ServerPort:     getEnvAsInt("SERVER_PORT", 8080),
		ModbusPort:     getEnvAsInt("MODBUS_PORT", 502),
		APIKey:         getEnvAsString("API_KEY", "default_api_key"),
		UpdateInterval: getEnvAsInt("UPDATE_INTERVAL", 5),
	}
}

func getEnvAsString(key, defaultValue string) string {
	if value, exists := os.LookupEnv(key); exists {
		return value
	}
	return defaultValue
}

func getEnvAsInt(key string, defaultValue int) int {
	if value, exists := os.LookupEnv(key); exists {
		if intValue, err := strconv.Atoi(value); err == nil {
			return intValue
		}
	}
	return defaultValue
}
